package com.mz.mattress.client.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.Card;
import com.mz.mattress.api.entity.CardMerchant;
import com.mz.mattress.api.entity.MemberCard;
import com.mz.mattress.api.util.DateUtil;
import com.mz.mattress.client.api.req.MemberCardReq;
import com.mz.mattress.client.mapper.CardMapper;
import com.mz.mattress.client.mapper.CardMerchantMapper;
import com.mz.mattress.client.mapper.MemberCardMapper;
import com.mz.mattress.client.service.MemberCardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;


@Service
@Slf4j
public class MemberCardServiceImpl implements MemberCardService {

    @Autowired
    private CardMapper cardMapper;

    @Autowired
    private CardMerchantMapper cardMerchantMapper;

    @Autowired
    private MemberCardMapper memberCardMapper;

    @Override
    public Result<IPage<MemberCard>> listPage(MemberCardReq req) {

        Page<MemberCard> listPage = memberCardMapper.listPage(req.newPage(), req.getMemberId(), req.getStatus());
        return Result.ok(listPage);

    }

    @Override
    public Result<List<MemberCard>> list(Long orderId) {
        List<MemberCard> list = memberCardMapper.selectByOrderId(orderId);
        return Result.ok(list);
    }

    @Override
    public Result<MemberCard> getById(Integer id) {

        MemberCard memberCard = memberCardMapper.selectById(id);

        Card card = cardMapper.selectById(memberCard.getCardId());
        memberCard.setRule(card.getRule());

        List<CardMerchant> list = cardMerchantMapper.selectByCardId(memberCard.getCardId());
        memberCard.setMerchantList(list);

        return Result.ok(memberCard);
    }

    @Override
    public Result sendCard(Integer id, Long memberId) {

        Card card = cardMapper.selectById(id);
        if (card == null) {
            return Result.fail("找不到卡项");
        }

        LocalDate endDate = LocalDate.parse(card.getEndDate());
        if (endDate.isBefore(LocalDate.now())) {
            return Result.fail("已过期");
        }

        MemberCard memberCard = new MemberCard();
        memberCard.setMemberId(memberId);
        memberCard.setCardId(id);
        memberCard.setMerchantId(card.getMerchantId());
        memberCard.setMerchantName(card.getMerchantName());
        memberCard.setName(card.getName());
        memberCard.setType(card.getType());
        memberCard.setMoney(card.getMoney());
        memberCard.setEndTime(card.getEndDate() + " 23:59:59");
        memberCard.setStatus(0);
        memberCardMapper.insert(memberCard);

        return Result.ok();
    }

    @Override
    public Result check(Integer id, Long memberId) {

        MemberCard card = memberCardMapper.selectById(id);
        if (card == null) {
            return Result.fail("找不到卡项");
        }

        if (!card.getMemberId().equals(memberId)) {
            return Result.fail("所属会员错误");
        }

        LocalDate endDate = LocalDate.parse(card.getEndTime().substring(0, 10));
        if (endDate.isBefore(LocalDate.now())) {
            return Result.fail("已过期");
        }

        MemberCard update = new MemberCard();
        update.setId(id);
        update.setUsedTime(DateUtil.getNowDateTime());
        //标记已使用
        update.setStatus(1);
        memberCardMapper.updateById(update);

        return Result.ok();
    }


}
